package encaps.action;

import encaps.entities.Grant;
import encaps.entities.PersonLocal;
import encaps.entities.PersonLocalHome;
import encaps.resources.Labels;
import encaps.utils.EntityUtil;
import encaps.utils.SystemException;
import encaps.utils.XMLHelper;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class ViewPersonAction extends Action {

    public static final String VIEW_PERSON = "viewPerson";
    private ActionContext actionContext;

    public ViewPersonAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }
    private Long id;

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            PersonLocalHome personHome = EntityUtil.getPersonHome();
            id = new Long(actionContext.getTextParameter(ID));
            PersonLocal person = personHome.findByPrimaryKey(id);
            Element userElement = XMLHelper.toXMLElement(person, Labels.getLabel(Labels.VIEW_PERSON), false, actionContext);
            return userElement;
        } catch (NamingException ex) {
            throw new ActionExecutionException("Cannot find PesronLocalHome object", ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException("Person " + id + " not found", ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException("System malfunction", ex);
        }
    }
}
